rangi = {
    [5] = "#a31303Manager",
    [4] = "#cc0000RCON",
    [3] = "#ff0000Administrator",
    [2] = "#006699Moderator",
    [1] = "#00CC00Supporter",
}

addCommandHandler("tt", function(plr,cmd,cel)
	if not getElementData(plr, "player:admin") then return end	
		if not cel then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /tt <NICK/ID>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr, 255, 0, 0,true)
			return
		end
		if isPedInVehicle(plr) then
			removePedFromVehicle(plr)
		end
		local x,y,z=getElementPosition(target)
		setElementInterior(plr,getElementInterior(target))
		setElementDimension(plr,getElementDimension(target))
		setElementPosition(plr,x+math.random(1,2),y+math.random(1,2),z)
end)

addCommandHandler("th", function(plr,cmd,cel)
	if not getElementData(plr, "player:admin") then return end	
		if not cel then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /th <NICK/ID>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr, 255, 0, 0,true)
			return
		end
		if isPedInVehicle(target) then
			removePedFromVehicle(target)
		end
		local x,y,z=getElementPosition(plr)
		setElementInterior(target,getElementInterior(plr))
		setElementDimension(target,getElementDimension(plr))
		if getElementDimension(plr) == 0 then
			setPlayerHudComponentVisible ( target, "radar", true )
		end
		outputChatBox("#ccc000➤ #ffffff"..rangi[tonumber(getElementData(plr,"player:level"))].." #808080"..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").." #ffffffprzeniósł Cię do siebie!", target, 255, 255, 255,true)
		setElementPosition(target,x+math.random(1,2),y+math.random(1,2),z)
end)

addCommandHandler("warn", function(plr,cmd,cel,...)
	if not getElementData(plr, "player:admin") then return end	
		local reason=table.concat({...}, " ")
		if not cel or not reason then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /warn <NICK/ID> <POWÓD>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr, 255, 0, 0,true)
			return
		end
        triggerClientEvent(target,"cmta_admin:gui_warn",target,reason,getPlayerName(plr))
		exports["cmta-db"]:dbSet("insert into cmta_acts (type,reason,uid,admin) VALUES(?,?,?,?)", "ostrzeżenie", reason, getElementData(target, "player:uid") ,getPlayerName(plr):gsub("#%x%x%x%x%x%x",""))
		triggerClientEvent(root,"addNotifications",root,"#808080"..getPlayerName(target):gsub("#%x%x%x%x%x%x","").."[ID:"..getElementData(target,"id").."] otrzymał(a) ostrzeżenie za #ccc000"..reason.." #808080[#ff0000od "..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").."#808080]")		
end)

addCommandHandler("k", function(plr,cmd,cel,...)
	if not getElementData(plr, "player:admin") then return end	
		local reason=table.concat({...}, " ")
		if not cel or not reason then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /k <NICK/ID> <POWÓD>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr, 255, 0, 0,true)
			return
		end
		exports["cmta-db"]:dbSet("insert into cmta_acts (type,reason,uid,admin) VALUES(?,?,?,?)", "wyrzucenie", reason, getElementData(target, "player:uid") ,getPlayerName(plr):gsub("#%x%x%x%x%x%x",""))
		triggerClientEvent(root,"addNotifications",root,"#808080"..getPlayerName(target):gsub("#%x%x%x%x%x%x","").."[ID:"..getElementData(target,"id").."] został(a) wyrzucony(a) za #ccc000"..reason.." #808080[#ff0000od "..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").."#808080]")		
		kickPlayer(target, plr, reason)
end)

addCommandHandler("inv", function(plr)
	if not getElementData(plr, "player:admin") then return end	
		if getElementAlpha(plr) > 0 then
			setElementAlpha(plr,0)
			setElementData(plr,"player:inv",true)
		else
			setElementAlpha(plr,255)
			setElementData(plr,"player:inv",false)
		end
end)

addCommandHandler("inf", function(plr,cmd,...)
	if not getElementData(plr, "player:admin") then return end
	if getElementData(plr, "player:level") == 5 then
		local text=table.concat({...}, " ")
		outputChatBox("♛ INFORMACJA ♛ #ffffff"..text, root, 0, 255, 255,true)
	end
end)

addCommandHandler("a", function(plr,cmd,...)
	if not getElementData(plr, "player:admin") then return end
	if getElementData(plr, "player:level") == 5 or getElementData(plr, "player:level") == 4 or getElementData(plr, "player:level") == 3 then
		local text=table.concat({...}, " ")
		outputChatBox("≥ "..text.." #808080// "..getPlayerName(plr):gsub("#%x%x%x%x%x%x",""), root, 255, 0, 0,true)
	end
end)

addCommandHandler("m", function(plr,cmd,...)
	if not getElementData(plr, "player:admin") then return end	
	if getElementData(plr, "player:level") == 1 then return end
		local text=table.concat({...}, " ")
		outputChatBox("≥ "..text.." #808080// "..getPlayerName(plr):gsub("#%x%x%x%x%x%x",""), root, 0, 102, 0,true)
end)

addCommandHandler("vtt", function(plr,cmd,vid)
	if not getElementData(plr, "player:admin") then return end	
		if not vid or not tonumber(vid) then return end
		vid=tonumber(vid)
		for i,v in ipairs(getElementsByType("vehicle")) do
			local dbid=getElementData(v,"vehicle:id")
			if dbid and tonumber(dbid) == vid then
	    		local x,y,z=getElementPosition(v)
	    		setElementPosition(plr,x,y,z+4)
	    		setElementInterior(plr, getElementInterior(v))
	    		setElementDimension(plr, getElementDimension(v))
	    		return
	    	end
		end
		local query=exports["cmta-db"]:dbGet("SELECT 1 FROM cmta_pojazdy WHERE id=? AND parking>0", vid)
		if query then
			outputChatBox("#FF3333[DUTY]#FF0000 Pojazd znajduje się w garażu lub przechowalni.", plr,255,255,255,true)
		else
			outputChatBox("#FF3333[DUTY]#FF0000 Nie znaleziono podanego pojazdu na mapie.", plr,255,255,255,true)
		end
end)

addCommandHandler("vth", function(plr,cmd,vid)
	if not getElementData(plr, "player:admin") then return end	
		if not vid or not tonumber(vid) then return end
		vid=tonumber(vid)
		for i,v in ipairs(getElementsByType("vehicle")) do
			local dbid=getElementData(v,"vehicle:id")
			if dbid and tonumber(dbid) == vid then
				if getElementData(v,"vehicle:police_parking") then outputChatBox("#FF3333[DUTY]#FFFFFF Pojazd znajduje się na parkingu policyjnym.", plr,255,0,0,true) return end
	    		local x,y,z=getElementPosition(plr)
	    		setElementPosition(v,x,y,z+0.1)
	    		setElementInterior(v, getElementInterior(plr))
	    		setElementDimension(v, getElementDimension(plr))
	    		return
	   		end
		end
		local query=exports["cmta-db"]:dbGet("SELECT 1 FROM cmta_pojazdy WHERE id=? AND parking>0", vid)
		if query then
			outputChatBox("#FF3333[DUTY]#FF0000 Pojazd znajduje się w garażu lub przechowalni.", plr,255,255,255,true)
		else
			outputChatBox("#FF3333[DUTY]#FF0000 Nie znaleziono podanego pojazdu na mapie.", plr,255,255,255,true)
		end
end)

addCommandHandler("zablokuj", function(plr,cmd,cel,...)
	if not getElementData(plr, "player:admin") then return end	
	if getElementData(plr, "player:level") == 5 or getElementData(plr, "player:level") == 4 then
		local reason=table.concat({...}, " ")
		if not cel or not reason then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /zablokuj <NICK/ID> <POWÓD>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr,255,255,255,true)
			return
		end
		local result = exports["cmta-db"]:dbGet("SELECT * FROM cmta_bans WHERE type=? AND uid=? LIMIT 1","blokada konta", getElementData(target,"player:uid"))
		if result and #result > 0 then
			return outputChatBox("#FF3333[DUTY]#FFFFFF Ten gracz posiada już blokadę konta.", plr, 255, 0, 0,true)
		end		
		reason2 = reason.." |od "..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").."|"
		exports["cmta-db"]:dbSet("INSERT INTO cmta_bans (nick,serial,uid,type,reason,time,banned_by) VALUES (?,?,?,?,?,?,?)", getPlayerName(target),getPlayerSerial(target),getElementData(target, "player:uid"),"blokada konta",reason:gsub("#%x%x%x%x%x%x",""), "9999-12-31 00:00:00", getPlayerName(plr))
		exports["cmta-db"]:dbSet("insert into cmta_acts (type,reason,uid,admin) VALUES(?,?,?,?)", "blokada konta", reason, getElementData(target, "player:uid") ,getPlayerName(plr):gsub("#%x%x%x%x%x%x",""))
		triggerClientEvent(root,"addNotifications",root,"#FF0000 Gracz "..getPlayerName(target).." został(a) zablokowany(a) za: "..reason2.."")		
		kickPlayer(target,plr,"Twoje konto zostało zablokowane, połącz się ponownie.")
	end
end)

addCommandHandler("ulecz", function(plr,cmd,cel)
	if not getElementData(plr, "player:admin") then return end	
		if not cel then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /ulecz <NICK/ID>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr,255,255,255,true)
			return
		end
		setElementHealth(target, 100)
		outputChatBox("Zostałeś(aś) uleczony(a) przez #ff0000"..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").."", target,255,255,255,true)
		outputChatBox("#FF3333[DUTY]#FFFFFF Uleczyłeś(aś) gracza #ff0000"..getPlayerName(target):gsub("#%x%x%x%x%x%x",""), plr,255,255,255,true)
end)

addCommandHandler("jp", function(plr,cmd)
	if not getElementData(plr, "player:admin") then return end	
		if isPedInVehicle(plr) then
			removePedFromVehicle(plr)
		end
		if doesPedHaveJetPack(plr) then
			removePedJetPack(plr)
		else
			givePedJetPack(plr)
		end
end)

addCommandHandler("b", function(plr,cmd,cel,x,bantype,...)
	if not getElementData(plr, "player:admin") then return end	
	if getElementData(plr, "player:level") == 5 or getElementData(plr, "player:level") == 4 or getElementData(plr, "player:level") == 3 or getElementData(plr, "player:level") == 2 then
		local reason=table.concat({...}, " ")
		if not cel or not tonumber(x) or not bantype or not reason then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /b <NICK/ID> <CZAS> <m/h/d> <POWÓD>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr,255,255,255,true)
			return
		end
		local reason2 = reason.." #808080[#ff0000od "..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").."#808080]"
		if bantype=="m" or bantype=="h" or bantype=="d" then
		if bantype=="m" then
		    exports["cmta-db"]:dbSet("INSERT INTO cmta_bans (nick,serial,uid,type,reason,time,banned_by) VALUES (?,?,?,?,?,NOW() + INTERVAL ? minute,?)", getPlayerName(target),getPlayerSerial(target),getElementData(target, "player:uid"),"ban",reason:gsub("#%x%x%x%x%x%x",""), x, getPlayerName(plr))
	    end
		if bantype=="h" then
			exports["cmta-db"]:dbSet("INSERT INTO cmta_bans (nick,serial,uid,type,reason,time,banned_by) VALUES (?,?,?,?,?,NOW() + INTERVAL ? hour,?)", getPlayerName(target),getPlayerSerial(target),getElementData(target, "player:uid"),"ban",reason:gsub("#%x%x%x%x%x%x",""), x, getPlayerName(plr))
		end
		if bantype=="d" then
			exports["cmta-db"]:dbSet("INSERT INTO cmta_bans (nick,serial,uid,type,reason,time,banned_by) VALUES (?,?,?,?,?,NOW() + INTERVAL ? day,?)", getPlayerName(target),getPlayerSerial(target),getElementData(target, "player:uid"),"ban",reason:gsub("#%x%x%x%x%x%x",""), x, getPlayerName(plr))
		end
		end
		exports["cmta-db"]:dbSet("insert into cmta_acts (type,reason,uid,admin) VALUES(?,?,?,?)", "ban", reason, getElementData(target, "player:uid") ,getPlayerName(plr):gsub("#%x%x%x%x%x%x",""))
		triggerClientEvent(root,"addNotifications",root,"#808080"..getPlayerName(target):gsub("#%x%x%x%x%x%x","").."[ID:"..getElementData(target,"id").."] został zbanowany na #ccc000"..x.." "..bantype.." #808080za #ccc000"..reason2.."")		
		kickPlayer(target,plr,"Zostałeś zbanowany, połącz się ponownie.")
	end
end)

addCommandHandler("spec", function(plr,cmd,cel)
	if not getElementData(plr, "player:admin") then return end	
		if not cel then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /spec <NICK/ID>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr,255,255,255,true)
			return
		end
		local x,y,z=getElementPosition(plr)
		removePedFromVehicle(plr)
		setElementInterior(plr, getElementInterior(target))
		setElementDimension(plr, getElementDimension(target))
		setCameraTarget(plr, target)
		setElementFrozen(plr,true)
		toggleAllControls(plr,false)
		toggleControl(plr,"chatbox",true)
		toggleControl(plr,"screenshot",true)
		if getElementAlpha(plr) > 0 then
			setElementAlpha(plr,0)
			destroyBlipsAttachedTo ( plr )
			setElementData(plr,"player:inv",true)
        end
		setElementData(plr,"spec:pos",{x,y,z})
end)

addCommandHandler("specoff", function(plr,cmd)
	if not getElementData(plr, "player:admin") then return end	
		local spec=getElementData(plr,"spec:pos")
		if not spec then return end
		setElementPosition(plr, spec[1], spec[2], spec[3])
		setElementData(plr,"spec:pos",nil)
		setCameraTarget(plr, plr)
		setElementFrozen(plr,false)
		toggleAllControls(plr,true)
		setElementAlpha(plr,255)
		setElementData(plr,"player:inv",false)
		createBlipAttachedTo ( plr, 0, 2, 255, 255, 255 )
end)

addCommandHandler("zpj", function(plr,cmd,cel,time,rodzaj,...)
	if not getElementData(plr, "player:admin") then return end	
		local reason=table.concat({...}, " ")
		if not cel or not tonumber(time) or not rodzaj or not reason then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /zpj <NICK/ID> <CZAS> <m/h/d> <POWÓD>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr,255,255,255,true)
			return
		end
		local result = exports["cmta-db"]:dbGet("SELECT * FROM cmta_bans WHERE type=? AND serial=? LIMIT 1","zabrane prawo jazdy", getPlayerSerial(target))
		if result and #result > 0 then
			return outputChatBox("#FF3333[DUTY]#FFFFFF Gracz Posiada zawieszone prawo jazdy do #ff0000"..result[1]["time"].."#ffffff za: #ff0000"..result[1]["reason"], plr, 255, 0, 0,true)
		end
		if isPedInVehicle(target) then
			removePedFromVehicle(target)
		end
		if tonumber(time) <= 0 then return end
		local reason2 = reason.." #808080[#ff0000od "..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").."#808080]"
		if rodzaj == "m" then
		trodzaj = "m"
		exports["cmta-db"]:dbSet("INSERT INTO cmta_bans (nick,serial,uid,type,reason,time,banned_by) VALUES (?,?,?,?,?,NOW() + INTERVAL ? minute,?)", getPlayerName(target),getPlayerSerial(target),getElementData(target, "player:uid"),"zabrane prawo jazdy",reason:gsub("#%x%x%x%x%x%x",""), time, getPlayerName(plr))
		elseif rodzaj == "h" then
		trodzaj = "h"
		exports["cmta-db"]:dbSet("INSERT INTO cmta_bans (nick,serial,uid,type,reason,time,banned_by) VALUES (?,?,?,?,?,NOW() + INTERVAL ? hour,?)", getPlayerName(target),getPlayerSerial(target),getElementData(target, "player:uid"),"zabrane prawo jazdy",reason:gsub("#%x%x%x%x%x%x",""), time, getPlayerName(plr))
		elseif rodzaj == "d" then
		trodzaj = "d"
		exports["cmta-db"]:dbSet("INSERT INTO cmta_bans (nick,serial,uid,type,reason,time,banned_by) VALUES (?,?,?,?,?,NOW() + INTERVAL ? day,?)", getPlayerName(target),getPlayerSerial(target),getElementData(target, "player:uid"),"zabrane prawo jazdy",reason:gsub("#%x%x%x%x%x%x",""), time, getPlayerName(plr))		if time == "1" then trodzaj = "dzień" else trodzaj = "dni" end	
		end
		exports["cmta-db"]:dbSet("insert into cmta_acts (type,reason,uid,admin) VALUES(?,?,?,?)", "prawo jazdy", reason, getElementData(target, "player:uid") ,getPlayerName(plr):gsub("#%x%x%x%x%x%x",""))
		triggerClientEvent(root,"addNotifications",root,"#808080"..getPlayerName(target):gsub("#%x%x%x%x%x%x","").."[ID:"..getElementData(target,"id").."] otrzymał(a) zakaz prowadzenia pojazdów na #ccc000"..time.." "..trodzaj.." #808080za #ccc000"..reason2.."")		
end)

addCommandHandler("sprawdzserial", function(plr,cmd,cel)
	if not getElementData(plr, "player:admin") then return end	
		if not cel then
			outputChatBox("#FF3333[DUTY]#FFFFFF Użycie: /sprawdzserial <NICK/ID>", plr,255,255,255,true)
			return
		end
		local target=exports["cmta-core"]:findPlayer(plr,cel)
		if not target then
			outputChatBox("#FF3333[DUTY]#FF0000 Nie odnaleziono takiego gracza.", plr,255,255,255,true)
			return
		end
		outputChatBox("#FF3333[DUTY]#FFFFFF Serial gracza: #ff0000"..getPlayerName(target):gsub("#%x%x%x%x%x%x","").." #ffffffto #ccc000"..getPlayerSerial(target).."", plr, 255, 0, 0,true)
end)

function destroyBlipsAttachedTo(player)
    local attached = getAttachedElements ( player )
    if ( attached ) then
        for k,element in ipairs(attached) do
            if getElementType ( element ) == "blip" then
                destroyElement ( element )
            end
        end
    end
end